package com.back.vueback.controller;

import com.back.vueback.entity.Product;
import com.back.vueback.repository.ProductRepository;
import lombok.AllArgsConstructor;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.UUID;

/**
 * @author wuyanshen
 * @date 2020-02-04 10:53 上午
 * @discription 描述
 */
@RestController
@AllArgsConstructor
public class ProductController {

    private final ProductRepository productRepository;

    @GetMapping("/product/{pageSize}/{pageNum}")
    public Page<Product> findAllProduct(@PathVariable int pageSize, @PathVariable int pageNum){
        PageRequest pageRequest = PageRequest.of(pageNum,pageSize);
        return productRepository.findAll(pageRequest);
    }

    @PostMapping("/product")
    public String saveProduct(@RequestBody Product product){
        String uuid = UUID.randomUUID().toString().replaceAll("-","");
        product.setId(uuid);
        Product pro = productRepository.save(product);
        if(pro!=null){
            return "success";
        }else{
            return "fail";
        }
    }

    @PutMapping("/product")
    public String updateProduct(@RequestBody Product product){
        Product pro = productRepository.save(product);
        if(pro!=null){
            return "success";
        }else{
            return "fail";
        }
    }

    @DeleteMapping("/product")
    public void deleteProduct(Product product){
        productRepository.delete(product);
    }

}
